﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key">



  <xsl:key name="Hours_By_BaseLine" match="BacklogItemHours" use="BaseLine"/>

  <xsl:variable name="ServerURL" select="/ProjectDataSet/General/ServerURL"/>

  <xsl:variable name="currentSprintNumber" select ="/ProjectDataSet/Project/Sprint[SprintUId = /ProjectDataSet/Project/CurrentSprintUId]/SprintNumber"/>
  <xsl:variable name="currentPlanSprintUId" select ="/ProjectDataSet/Project/Sprint[SprintNumber = /ProjectDataSet/Project/Baseline]/SprintUId"/>

  <xsl:include href="../../Style.xslt"/>




  <xsl:template name="AlternateColorTD">
    <xsl:param name="text"/>
    <xsl:param name="number"/>
    <xsl:param name="color">Black</xsl:param>
    <xsl:param name="align">Left</xsl:param>
    <xsl:param name="colspan" select="1"/>
    <xsl:param name="current" select="0"/>

    <xsl:variable name="style">
      <xsl:choose>
        <xsl:when test="$current = 1">{StaticResource CurrentCell}</xsl:when>
        <xsl:when test="$number mod 2 = 0">{StaticResource AlternateCell2}</xsl:when>
        <xsl:otherwise>{StaticResource AlternateCell1}</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="validColspan">
      <xsl:choose>
        <xsl:when test="$colspan &gt; 0">
          <xsl:value-of select="$colspan"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="1"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <TableCell ColumnSpan="{$validColspan}" Style="{$style}">
      <Paragraph TextAlignment="{$align}" Foreground="{$color}">
        <xsl:value-of select="$text"/>
      </Paragraph>
    </TableCell>


  </xsl:template>





  <xsl:template name="ImpactSignal">
    <xsl:param name="impact" />

    <xsl:choose>
      <xsl:when test="$impact = 0">
        <Image Source="{$ServerURL}/FactoryService/Images/RiskIndicators/GreenSquare.png" Width="32" Height="32"/>
        <LineBreak/>
        NONE
      </xsl:when>
      <xsl:when test="$impact = 1">
        <Image Source="{$ServerURL}/FactoryService/Images/RiskIndicators/BlueSquare.png" Width="32" Height="32"/>
        <LineBreak/>
        LOW
      </xsl:when>
      <xsl:when test="$impact = 2">
        <Image Source="{$ServerURL}/FactoryService/Images/RiskIndicators/YellowSquare.png" Width="32" Height="32"/>
        <LineBreak/>
        MEDIUM
      </xsl:when>
      <xsl:when test="$impact = 3">
        <Image Source="{$ServerURL}/FactoryService/Images/RiskIndicators/RedSquare.png" Width="32" Height="32"/>
        <LineBreak/>
        HIGH
      </xsl:when>
    </xsl:choose>

  </xsl:template>

  <xsl:template name="BacklogItemStatus">
    <xsl:choose>
      <xsl:when test="Status = 0">
        PLANNED
      </xsl:when>
      <xsl:when test="Status = 1">
        WORKING
      </xsl:when>
      <xsl:when test="Status = 2">
        DONE
      </xsl:when>
      <xsl:when test="Status = 3">
        CANCELED
      </xsl:when>

    </xsl:choose>

  </xsl:template>






  <xsl:template name="ProjectOverCheck">
    <xsl:if test="not($currentSprintNumber)">
      <BlockUIContainer>
        <Border Style="{{StaticResource AlertBorder}}" >
          <TextBlock FontSize="12">
            <Bold>There are no Sprint running now.</Bold>
          </TextBlock>
        </Border>
      </BlockUIContainer>
    </xsl:if>
  </xsl:template>


  <xsl:template name="BaseLineCheck">    
    <xsl:if test="/ProjectDataSet/Project/Baseline &lt; $currentSprintNumber">
      <BlockUIContainer>
        <Border Style="{{StaticResource AlertBorder}}" >
          <TextBlock FontSize="12">
            <Bold>
              Sprint
              <xsl:value-of select="$currentSprintNumber"/> has not been planned yet.
            </Bold><LineBreak/><LineBreak/>
            The backlog items from Sprint
            <xsl:value-of select="$currentSprintNumber"/>
            should be priorized, measured in size and estimated by the project team.
          </TextBlock>
        </Border>
      </BlockUIContainer>
    </xsl:if>
    <xsl:if test="/ProjectDataSet/Project/Baseline = 0">
      <BlockUIContainer>
        <Border Style="{{StaticResource AlertBorder}}" >
          <TextBlock  FontSize="12">
            <Bold>
              The first Sprint of the project has not been planned yet.
            </Bold>
              <LineBreak/><LineBreak/>
              Please, plan your Sprint first in order to see your project backlog.
            
          </TextBlock>
        </Border>
      </BlockUIContainer>
    </xsl:if>
  </xsl:template>

  <xsl:template name="StatusCell">
    <xsl:variable name="style">
      <xsl:choose>
        <xsl:when test="Status = 2">{StaticResource BacklogItemDoneCell}</xsl:when>
        <xsl:when test="Status = 1">{StaticResource BacklogItemWorkingCell}</xsl:when>
        <xsl:when test="Status = 0">{StaticResource BacklogItemRequiredCell}</xsl:when>
        <xsl:otherwise>{StaticResource BacklogItemCanceledCell}</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <TableCell Style="{$style}">
      <Paragraph TextAlignment="Center">        
          <xsl:choose>
            <xsl:when test="Status = 2">D</xsl:when>
            <xsl:when test="Status = 1">W</xsl:when>
            <xsl:when test="Status = 3">C</xsl:when>
            <xsl:otherwise>R</xsl:otherwise>
          </xsl:choose>        
    </Paragraph>
    </TableCell>
  </xsl:template>

  <xsl:template name="PlanFooter">
    <xsl:param name="plannedItems"/>
    <xsl:param name="plannedSprints"/>
    <xsl:param name="sprintNumber"/>
    <xsl:param name="expirationDateString"/>

    <TableRow Style="{{StaticResource GroupRow}}">
      <TableCell ColumnSpan="4">
        <Paragraph>
          <Bold>
            End of Sprint <xsl:value-of select="$sprintNumber"/> (<xsl:value-of select="$expirationDateString"/>)
          </Bold>          
        </Paragraph>
      </TableCell>
      <TableCell>
        <Paragraph TextAlignment="Right">
          <xsl:value-of select="sum($plannedItems/Size)"/>
        </Paragraph>
      </TableCell>
      <xsl:for-each select="$plannedSprints">
        <xsl:sort select="SprintNumber" data-type="number" order="descending" />
        <xsl:variable name="baseline" select="SprintNumber"/>

        <xsl:variable name="plannedUnits"
                      select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$baseline)[Quantity &gt; 0]/UnitName]"/>


        <xsl:for-each select="$plannedUnits">
          <xsl:sort select="UnitName" />
          <xsl:variable name="unitName" select="UnitName" />

          <xsl:variable name="border">
            <xsl:choose>
              <xsl:when test="position() = last()">{StaticResource RightBorderCell}</xsl:when>
              <xsl:otherwise>{StaticResource NormalCell}</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <TableCell Style="{$border}">
            <Paragraph TextAlignment="Right">
              <xsl:value-of select="sum($plannedItems/BacklogItemHours[UnitName=$unitName and BaseLine=$baseline]/Quantity)"/>  
            </Paragraph>
          </TableCell>
        </xsl:for-each>
        
      </xsl:for-each>
    </TableRow>
    
  </xsl:template>

  <xsl:template name="BacklogFooter">    
    <xsl:param name="plannedSprints"/>

    <TableRow Style="{{StaticResource HeaderRow}}">
      <TableCell ColumnSpan="4">
        <Paragraph>
          <Bold>
            End of Project
          </Bold>          
        </Paragraph>
      </TableCell>
      <TableCell>
        <Paragraph TextAlignment="Right">
          <xsl:value-of select="sum(/ProjectDataSet/Project/BacklogItem/Size)"/>
        </Paragraph>
      </TableCell>
      <xsl:for-each select="$plannedSprints">
        <xsl:sort select="SprintNumber" data-type="number" order="descending" />
        <xsl:variable name="baseline" select="SprintNumber"/>


        <xsl:variable name="plannedUnits"
                      select="../BacklogItemUnit[UnitName = key('Hours_By_BaseLine',$baseline)[Quantity &gt; 0]/UnitName]"/>


        <xsl:for-each select="$plannedUnits">
          <xsl:sort select="UnitName" />
          <xsl:variable name="unitName" select="UnitName" />
          <xsl:variable name="border">
            <xsl:choose>
              <xsl:when test="position() = last()">{StaticResource RightBorderCell}</xsl:when>
              <xsl:otherwise>{StaticResource NormalCell}</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <TableCell Style="{$border}">
            <Paragraph TextAlignment="Right">
              <xsl:value-of select="sum(/ProjectDataSet/Project/BacklogItem/BacklogItemHours[UnitName=$unitName and BaseLine=$baseline]/Quantity)"/>
            </Paragraph>
          </TableCell>
        </xsl:for-each>        
      </xsl:for-each>
    </TableRow>
  </xsl:template>

  <xsl:template name="DeliveryItems">
    <xsl:param name="plannedItems"/>
    <xsl:param name="sprintUId"/>

    <xsl:if test="count($plannedItems)=0">
      <Paragraph>There are no items deliveried at the Sprint.</Paragraph>
    </xsl:if>

    <xsl:if test="count($plannedItems) &gt; 0">
      <Table Style="{{StaticResource NoBorderTable}}">

        <Table.Columns>
          <TableColumn Width="25" />
          <TableColumn />
          <TableColumn Width="80" />
          <TableColumn Width="80" />
        </Table.Columns>

        <TableRowGroup>

          <TableRow>
            <TableCell Style="{{StaticResource DoubleLineCell}}" ColumnSpan="2">
              <Paragraph>
                <Bold>Item</Bold>
              </Paragraph>
            </TableCell>
            <TableCell Style="{{StaticResource DoubleLineCell}}">
              <Paragraph TextAlignment="Center">
                <Bold>Effort (hrs)</Bold>
              </Paragraph>
            </TableCell>
            <TableCell Style="{{StaticResource DoubleLineCell}}">
              <Paragraph TextAlignment="Center">
                <Bold>Status</Bold>
              </Paragraph>
            </TableCell>
          </TableRow>




          <!-- BACKLOG ITENS -->
          <xsl:for-each select="$plannedItems">
            <xsl:sort select="BusinessValue" data-type="number" order="descending" />
            <xsl:sort select="BacklogItemNumber" data-type="number" />
            <xsl:sort select="Title" />

            <xsl:variable name="backlogItemUId" select="BacklogItemUId" />

            <xsl:variable name="sprintNumber" select="/ProjectDataSet/Project/Sprint[SprintUId=$sprintUId]/SprintNumber"/>

            <TableRow >
              <TableCell Style="{{StaticResource SingleLineCell}}">
                <Paragraph>
                  <xsl:value-of select="BacklogItemNumber" />
                </Paragraph>
              </TableCell>
              <TableCell Style="{{StaticResource SingleLineCell}}">
                <Paragraph>
                  <xsl:value-of select="Title" />
                </Paragraph>
              </TableCell>
              <TableCell Style="{{StaticResource SingleLineCell}}">
                <Paragraph TextAlignment="Center">
                  <!--<xsl:value-of select="sum(BacklogItemHours[SprintUId=$sprintUId and BaseLine=$sprintNumber]/Quantity)"/>-->
                  <xsl:choose>
                    <xsl:when test="$sprintNumber &gt;= /ProjectDataSet/Project/Baseline">
                      <xsl:value-of select="sum(BacklogItemHours[SprintUId=$sprintUId and BaseLine=/ProjectDataSet/Project/Baseline]/Quantity)"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="sum(BacklogItemHours[SprintUId=$sprintUId and BaseLine=$sprintNumber]/Quantity)"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </Paragraph>
              </TableCell>
              <TableCell Style="{{StaticResource SingleLineCell}}">
                <Paragraph TextAlignment="Center">
                  <xsl:call-template name="BacklogItemStatus"/>
                </Paragraph>
              </TableCell>

            </TableRow>


          </xsl:for-each>

        </TableRowGroup>
      </Table>
    </xsl:if>

  </xsl:template>



</xsl:stylesheet>

